﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Runtime.InteropServices;

namespace 高频打印
{
    public partial class FormRepeatPrint : Form
    {
        #region "声明变量"
        [DllImport("RFIDCARD.dll")]
        private extern static int OpenReader(int iport, int cardtype, ref int ihHandle);
        [DllImport("RFIDCARD.dll")]
        private extern static void CloseReader(int hHandle);
        [DllImport("RFIDCARD.dll")]
        private extern static int GetCard(int hHandle);
        [DllImport("RFIDCARD.dll")]
        private extern static int CardReadID(int hHandle, ref byte idstr, int idsize, ref int idlen);
        [DllImport("RFIDCARD.dll")]
        private extern static int CardReadIDStr(int hHandle, ref byte idstr, int idsize);
        [DllImport("RFIDCARD.dll")]
        private extern static int CardRead(int hHandle, byte blocknum, ref byte data);
        [DllImport("RFIDCARD.dll")]
        private extern static int CardReadStr(int hHandle, byte blocknum, ref byte data);
        [DllImport("RFIDCARD.dll")]
        private extern static int CardWrite(int hHandle, byte blocknum, string data);
        [DllImport("RFIDCARD.dll")]
        private extern static int CardWriteStr(int hHandle, byte blocknum, string data);
        [DllImport("RFIDCARD.dll")]
        private extern static int UartWrite(int hHandle, string buf, int buflen);
        [DllImport("RFIDCARD.dll")]
        private extern static int UartRead(int hHandle, ref byte buf, int buflen);
        #endregion

        //定义打印机状态，0表示正常，1表示异常
        int PrinterState = 0;

        MySqlConnection conn = new MySqlConnection("data source=localhost;database=test;user id=root;password=root;pooling=false;charset=utf8");

        public FormRepeatPrint()
        {
            InitializeComponent();
        }

        private void FormSinglePrint_Load(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Hide();
            FormLogin FormMain = new FormLogin(); //自己改成登录框名称
            FormMain.ShowDialog();
            this.Close();
        }

        private void FormSinglePrint_Activated(object sender, EventArgs e)
        {
            textBox1.Focus();
        }


        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)Keys.Enter)
            {
                return;
            }
            pictureBox1.Visible = false;
            pictureBox2.Visible = true;
            pictureBox3.Visible = false;
            label3.Text = "开始标签打印";
            string sql = "select * from goods_Table where 条码='" + textBox1.Text + "'";
            DataTable dtShow = new DataTable();
            MySqlDataAdapter sda = new MySqlDataAdapter(sql, conn);
            sda.Fill(dtShow);

            //查询数据是否存在
            if (dtShow.Rows.Count < 1)
            {
                label3.Text = "数据库中无此记录，无法打印";
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
                pictureBox3.Visible = true;
                return;

            }

            //查询终端编码是否正确
            if (dtShow.Rows[0]["终端编码"].ToString() != Class1.storeID.ToString())
            {
                label3.Text = "门店ID不符，无法打印";
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
                pictureBox3.Visible = true;

                textBox1.Focus();
                textBox1.SelectAll();
                return;
            }

            //查询goods_Table数据表里标签是否打印

            if (dtShow.Rows[0]["打印状态"].ToString() != "0")
            {
                label3.Text = "该标签已经打印，无法重复打印";
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
                pictureBox3.Visible = true;

                textBox1.Focus();
                textBox1.SelectAll();
                return;
            }
            //回退

            //寻卡

            //取标签ID

            //写入条码数据到标签

            //读取验证写入数据

            //打印标签

            //更新Goods_Table的打印状态

            //插入中间表

            //完成打印

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void FormRepeatPrint_Load(object sender, EventArgs e)
        {

        }

    }
}
